package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.wordOrder.ImWorkOrderDTO;
import com.etone.smartAudit.dto.wordOrder.WorkOrderDTO;
import com.etone.smartAudit.service.WorkOrderService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.workOrder.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author 张任豪
 */
@RestController
@AllArgsConstructor
@Api(tags = "工单列表", value = "WorkOrderController")
public class WorkOrderController {

    private WorkOrderService workOrderService;

    @ApiOperation(value = "保存或派发工单")
    @PostMapping("/workOrder/add")
    public ResponseVO add(
            @ApiParam(value = "工单信息") WorkOrderDTO dto,
            @ApiParam(value = "派发审核人账号") @RequestParam(value = "account", required = false) String userID,
            @ApiIgnore @LoginAdmin Admin admin) {
        return workOrderService.add(dto, admin, userID);
    }

    @ApiOperation(value = "修改工单")
    @PutMapping("/workOrder/update")
    public void update(
            @ApiParam(value = "id") @RequestParam Integer id,
            @ApiParam(value = "工单信息") WorkOrderDTO dto,
            @ApiParam(value = "派发审核人账号") @RequestParam(value = "account", required = false) String userID,
            @ApiIgnore @LoginAdmin Admin admin) {
        workOrderService.update(dto, admin, id, userID);
    }

    @ApiOperation(value = "工单详情")
    @GetMapping("/workOrder/detail")
    public WorkOrderDetailVO detail(@ApiParam(value = "id") @RequestParam("id") Integer id) {
        return workOrderService.detail(id);
    }

    @ApiOperation(value = "工单批量删除")
    @DeleteMapping("/workOrder/delete/{ids}")
    public void delete(@ApiParam("ids") @PathVariable List<Integer> ids, @RequestParam("msg") String msg) {
        workOrderService.deleteAll(ids, msg);
    }

    @ApiOperation(value = "工单列表")
    @GetMapping("/workOrder/list")
    public PagerVO<WorkOrderVO> list(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize,
            @ApiParam(value = "项目名称") @RequestParam(required = false) String name,
            @ApiParam(value = "整改编号") @RequestParam(required = false) String rectifyCode,
            @ApiParam(value = "开始日期") @RequestParam(required = false) Date startDate,
            @ApiParam(value = "结束日期") @RequestParam(required = false) Date endDate,
            @ApiParam(value = "工单状态(0未开始,1进行中,2办结)") @RequestParam(required = false) Integer status,
            @ApiParam(value = "整改是否超期(0否,1是)") @RequestParam(required = false) Integer isOverdue,
            @ApiParam(value = "工单接收人") @RequestParam(required = false) String wordOrderName,
            @ApiParam(value = "关键字") @RequestParam(required = false) String keyword
    ) {
        return workOrderService.listWorkOrder(page, pageSize, name, rectifyCode, startDate, endDate, status, isOverdue, wordOrderName, keyword, admin);
    }

    @ApiOperation(value = "批量导入工单")
    @PostMapping("/workOrder/import")
    public ImportWorkVO importWork(@ApiParam(value = "导入文件") @RequestPart("file") MultipartFile file) {
        return workOrderService.importWork(file);
    }

    @ApiOperation(value = "附件上传")
    @PostMapping("/workOrder/fileUpload")
    public OrderFileVO fileUpload(@ApiParam(value = "上传附件") @RequestPart("file") MultipartFile file) {
        return workOrderService.fileUpload(file);
    }

    @ApiOperation(value = "批量添加工单")
    @PostMapping("/workOrder/batchAddOrder")
    public void batchAddOrder(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "工单集合") @RequestBody List<ImWorkOrderDTO> imWorkOrderDTOS
    ) {
        workOrderService.batchAddOrder(imWorkOrderDTOS, admin);
    }

    @ApiOperation(value = "工单导入模板下载")
    @GetMapping("/workOrder/orderTemplate")
    public void orderTemplate(HttpServletResponse response) {
        workOrderService.orderTemplate(response);
    }

    @ApiOperation(value = "判断是否有权限修改该工单")
    @GetMapping("/workOrder/isPermissionUpdate")
    public ResponseVO isPermissionUpdate(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "工单id") @RequestParam("id") Integer id) {
        return workOrderService.isPermissionUpdate(id, admin);
    }

}
